using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;

namespace SimianReshaper
{
	public class Preferences
	{
		#region Fields
		bool ignoreDesignerErrors;
		bool ignoreIgnoreList;
		Collection<string> ignoreList;
		int threshold;
		#endregion

		#region Properties
		public bool IgnoreDesignerErrors
		{
			get { return ignoreDesignerErrors; }
			set { ignoreDesignerErrors = value; }
		}

		public bool IgnoreIgnoreList
		{
			get { return ignoreIgnoreList; }
			set { ignoreIgnoreList = value; }
		}

		public Collection<string> IgnoreList
		{
			get { return ignoreList; }
			set { ignoreList = value; }
		}

		public int Threshold
		{
			get { return threshold; }
			set { threshold = value; }
		}
		#endregion

		#region Constructors
		public Preferences()
		{
			this.ignoreDesignerErrors = true;
			this.ignoreList = new Collection<string>();
			this.threshold = 30;
		}
		#endregion

		#region Public methods
		public void Serialize(string xmlPath)
		{
			StreamWriter streamWriter = null;
			try
			{
				streamWriter = new StreamWriter(xmlPath);
				XmlSerializer xs = new XmlSerializer(typeof(Preferences));
				xs.Serialize(streamWriter, this);
			}
			catch (System.IO.IOException)
			{

			}
			finally
			{
				if (streamWriter != null)
				{
					streamWriter.Close();
				}
			}
		}

		public static Preferences Deseralize(string xmlPath)
		{
			Preferences preferences = null;
			StreamReader streamReader = null;
			try
			{
				streamReader = new StreamReader(xmlPath);
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(Preferences));
				preferences = xmlSerializer.Deserialize(streamReader) as Preferences;
			}
			catch (System.IO.IOException)
			{
				preferences = new Preferences();
				preferences.Serialize(xmlPath);
			}
			finally
			{
				if (streamReader != null)
				{
					streamReader.Close();
				}
			}
			return preferences;
		}

		public Preferences Clone()
		{
			Preferences clone = new Preferences();
			clone.ignoreDesignerErrors = this.ignoreDesignerErrors;
			clone.ignoreIgnoreList = this.ignoreIgnoreList;
			clone.ignoreList = new Collection<string>();
			clone.threshold = this.threshold;
			foreach (string ignoreEntiy in this.ignoreList)
			{
				clone.ignoreList.Add(ignoreEntiy);
			}
			return clone;
		}
		#endregion
	}
}
